package com.atguigu.study.controller;

import cn.hutool.core.util.IdUtil;
import com.atguigu.study.entities.Order;
import com.atguigu.study.entities.User;
import com.atguigu.study.exp.BusinessException;
import com.atguigu.study.resp.ResultData;
import com.atguigu.study.resp.ReturnCodeEnum;
import com.atguigu.study.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping(value = "/user/ad")
    public ResultData<Boolean> addd(@RequestBody Order order){
        for (int i = 0; i < 20; i++) {
            Order domain = new Order();
            domain.setUsername(order.getUsername()+ IdUtil.simpleUUID().substring(0,4));
            domain.setPassword("666666");
            domain.setSex(1);
            orderService.save(domain);
        }
        return ResultData.success(Boolean.TRUE);
    }
    @DeleteMapping(value = "/user/de/{id}")
    private ResultData<Boolean> delet(@PathVariable("id")Integer id){
        orderService.removeById(id);
        return ResultData.success(Boolean.TRUE);
    }

    @PutMapping(value = "/user/up")
    public ResultData<Boolean> update(@RequestBody Order order){
        Order oldorder = orderService.getById(order.getId());
        LambdaUpdateWrapper<Order> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(Order::getUsername,order.getUsername())
                .set(Order::getPassword,order.getPassword())
                .set(Order::getSex,order.getSex())
                .set(Order::getVersion,oldorder.getVersion()+1)
                .eq(Order::getId,order.getId())
                .eq(Order::getVersion,oldorder.getVersion());
        return ResultData.success(orderService.update(updateWrapper));
    }

    @PutMapping(value = "/user/updatetwo")
    public ResultData<Boolean> updateConcurrent(@RequestBody Order order){
        Order order1 = orderService.getById(order.getId());
        Order order2 = orderService.getById(order.getId());
        order1.setPassword("xxxxxx");
        orderService.updateById(order1);
        order2.setPassword("yyyyyy");
        boolean b = orderService.updateById(order2);
        if (!b)
            {
                System.out.println("oldorder2=====update fail");
                return ResultData.success(Boolean.FALSE);
            }else{
                System.out.println("oldorder2=====update success");
                return ResultData.success(Boolean.TRUE);
            }
    }

    @GetMapping(value = "/user/getla/{id}")
    public ResultData<Order> getById(@PathVariable("id") Integer id)
    {
        if(id == -4) throw new RuntimeException("id不能为负数");

        if (id < 0) {
            throw new BusinessException(ReturnCodeEnum.REQPARAM_NEGATIVE.getCode(),ReturnCodeEnum.REQPARAM_NEGATIVE.getMessage());
        }
        return ResultData.success(orderService.getById(id));
    }
    @GetMapping(value = "/user/listall")
    public ResultData<List<Order>> list()
    {
        return ResultData.success(orderService.list());
    }

    @GetMapping(value = "/user/geta")
    public ResultData<Order> getByUseName(@RequestParam("username") String username)
    {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getUsername,username);

        return ResultData.success(orderService.getOne(queryWrapper));
    }
    @GetMapping("/user/selectPages")
    public List<Order> selectPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize)
    {
        IPage<Order> page = new Page(pageNum,pageSize);
        return orderService.page(page).getRecords();
    }
    @RequestMapping(value = "/pay/errors",method = RequestMethod.GET)
    public ResultData<Integer> getPayError()
    {
        Integer i = Integer.valueOf(200);
        try
        {
            System.out.println("--------come here");
            int data = 10/0;
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
        }
        return ResultData.success(i);
    }
}
